package com.team5101.service.impl;

import com.team5101.dao.ISeatDao;
import com.team5101.entity.Seat;
import com.team5101.entity.VO.SeatInfoVO;
import com.team5101.service.ISeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

@Service
public class SeatServiceImpl implements ISeatService {

    @Autowired
    private ISeatDao iSeatDao;

    /**
     * 根据厅号,放映id查询 当前已被购买的的座位号
     * hallId 厅号   tid    放映id
     */
    public List<Integer> queryBuySeatByID( Integer tId,Integer hallId,Integer stateType) {
        if(hallId==null||tId==null||stateType==null){
            return null;
        }
        return iSeatDao.queryBuySeatByID(tId,hallId,stateType);
    }

    /**
     *  根据厅号查询当前数据量
     *  hallId 厅号
     */
    public Integer queryNumByhallId(Integer hallId) {
        return iSeatDao.queryNumByhallId(hallId);
    }

    /**
     * 根据厅号 状态值 放映id 插入座位号
     */
    @Transactional
    public Integer InsertSeatByHallID( SeatInfoVO seatInfoVO) {
        List<Integer> nums = seatInfoVO.getNumSeat();

        List<Seat> seatList=new ArrayList<Seat>();
        Integer hallId = seatInfoVO.getHallId();
        Integer stateType = seatInfoVO.getStateType();
        Integer tid=seatInfoVO.getSid();
        for(Integer n:nums){
            Seat st=new Seat();
            st.setsNnum(n);
            st.setsHallid(hallId);
            st.setsState(stateType);
            st.setsTid(tid);
            seatList.add(st);
        }

        int num=iSeatDao.insertSeatByHallID(seatList);
        System.out.println(num);
        return num;
    }

    /**
     * 根据厅号 状态值 放映id 修改座位状态
     */
    @Transactional
    public Integer UpdataSeatState(SeatInfoVO seatInfoVO) {

        List<Integer> nums = seatInfoVO.getNumSeat();
        Integer hallId = seatInfoVO.getHallId();
        int num=0;
        Integer stateType = seatInfoVO.getStateType();
        Integer tid=seatInfoVO.getSid();
        for(Integer n:nums){
            Seat st=new Seat();
            st.setsNnum(n);
            st.setsHallid(hallId);
            st.setsState(stateType);
            st.setsTid(tid);
            num+=iSeatDao.updateSeat(st);
        }
        System.out.println(num);
        return num;
    }

    /**
     * 根据厅号hallId 状态值 放映id 删除座位信息
     */
    public Integer deleteSeat(SeatInfoVO seatInfoVO) {
        List<Integer> seatId=new ArrayList<Integer>();

        List<Integer> nums =seatInfoVO.getNumSeat();
        nums.add(69);
        nums.add(70);
        Integer hallId = seatInfoVO.getHallId();
        int num=0;
        Integer stateType =seatInfoVO.getStateType();
        Integer tid=seatInfoVO.getSid();
        List<Seat> seatList = iSeatDao.querySeat(tid,hallId,stateType);
        System.out.println(seatList);
        for(Seat s:seatList ){
            for(Integer n:nums){
                if(n==s.getsNum())
                seatId.add(s.getId());

            }
        }
        return iSeatDao.deleteSeat(seatId);
    }
}
